package com.fy.common.util;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import com.fy.framework.common.utils.AesEncodeUtil;
import com.fy.framework.common.utils.JwtToken;

import io.jsonwebtoken.Claims;

/**
 * 从 {@code HttpServletRequest} 获取一些信息
 *
 * @author zhshuo
 * @date 2021/12/8 18:15
 */
@SuppressWarnings("all")
public final class TokenUtil {

    /**
     * 获取当前请求中的用户ID
     *
     * @return
     */
    public static Long getUserId() {
        return getUserId(getRequest());
    }

    private static HttpServletRequest getRequest() {
        ServletRequestAttributes servletRequestAttributes =
                (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        return servletRequestAttributes.getRequest();
    }

    public static Long getUserId(HttpServletRequest request) {
        return getUserIdFromToken(getToken(request));
    }

    public static <T> T getParamsFromToken(String token, String claimKey, Class<T> clazz) {
        Claims claims = JwtToken.getClaimFromToken(token);
        return claims.get(claimKey, clazz);
    }

    public static <T> T getParamsFromToken( String claimKey, Class<T> clazz) {
        String token = getToken(getRequest());
        Claims claims = JwtToken.getClaimFromToken(token);
        return claims.get(claimKey, clazz);
    }

    public static String getToken(HttpServletRequest httpServletRequest) {
        String authorization = httpServletRequest.getHeader("token");
        if (StringUtils.isBlank(authorization)) {
            authorization = httpServletRequest.getHeader("Authorization");
            if (StringUtils.isNotBlank(authorization)) {
                authorization = AesEncodeUtil.decrypt(authorization);
            }
        }

        return authorization;
    }

    public static Long getUserIdFromToken(String token) {
        if (token == null) {
            return -1L;
        } else {
            Long paramsFromToken = null;
            try {
                paramsFromToken = (Long) getParamsFromToken(token, "userId", Long.class);
            } catch (Exception var5) {
                String paramsFromToken2 = (String) getParamsFromToken(token, "userId", String.class);
                if (StringUtils.isNotBlank(paramsFromToken2)) {
                    paramsFromToken = Long.parseLong(paramsFromToken2);
                }
            }

            return paramsFromToken;
        }
    }

    public static Long getUserIdFromEnToken(String token) {
        if (token == null) {
            return -1L;
        } else {
            Long paramsFromToken = null;
            try {
                token = AesEncodeUtil.decrypt(token);
                paramsFromToken = (Long) getParamsFromToken(token, "userId", Long.class);
            } catch (Exception var5) {
                String paramsFromToken2 = (String) getParamsFromToken(token, "userId", String.class);
                if (StringUtils.isNotBlank(paramsFromToken2)) {
                    paramsFromToken = Long.parseLong(paramsFromToken2);
                }
            }

            return paramsFromToken;
        }
    }


    public static void main(String[] args) {
        Long s  = getUserIdFromEnToken("eoso6XL188CZlNicw7RjoYUcz09djsT/E6i1y/zKleYlvhTpX4wAAV3b1Rk/u4M9W27HhoedHWJmZvuHeeVBHaOeir4RcMfJBB1I6tpLbJXGm0zQkp5KTp9nNxzbN7MBQ21gOr9T4wqPliz2cw3JvOyoqm/yClri988yXHFbc2NhFthkrKqRYEsgFMEH9IZHj8KxwmQCtq1wfOEd/Omabw7OEE5gUaz2rO3eGWZ1TvlKQ8ISq15lq7aC2VKcFpUgUEiwBbLjAbySmof4BDENeWmPgz5Sl5qIrYgUi6hqzzeQ4T3C+3GUm2EJauss0E9auAjDG5pKjeRRCSj+ssb43JfwnNzc1vQUSaDDfjqUi70YlQxkgfL9Zx6+EJ6LHQNcig0n7vOlXkBdEEqhW8Ux/8ojSYesNOljVcsAsTjhtggSbUxCtOm6Xpnx0JfZKNsIiZa7zUvAJeNm1ID5imTUqnk0q4iSxMlOxAM41eYkbyRfk6jElBOg7SMPXp5KykaxIQ7T24M0oy9WmkjyQCUOHOkKyfOUZtqo/NVi7k1Lujsr69jQeBN4YU2NvfcE85fbJuWMw2973HCbSP0voIsxYWCE+b+is6rbshKo4mJpfyz4L0d/UMSDZbrZFqYraujHRUGe421kvhmOKi5bjMwt8JK01/EalA3EmKwu+05SG3cMsDXCVjvxXoKBUOCXkdzlx5cPvBP8I0LsZ42tRqn81AEi4L6Jolx/odtWJs1u3f89exM3vC9xYbprpuzi39vhu1RdVtppgrvZE3S1wHrlo4N1QCxmbN0tYKEPvX3MbVnqDb1XjVC9Cat8sWfXqBLtdgJcwhL2fgdr7GNOXg8rilqFdQtYm34dHJhVooroowM4YvdYMYX6/rjNhfEhu1E2iOWT7BbbWkqAfTMMILtT3Q==");

        System.out.println(s);
    }

}
